import 'dart:io';
import 'package:abc/view/CustomAppBar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:video_player/video_player.dart';

class VideoPlayerPage extends StatefulWidget {
  final String videoPath;
  
  const VideoPlayerPage({Key? key, required this.videoPath}) : super(key: key);

  @override
  State<VideoPlayerPage> createState() => _VideoPlayerPageState();
}

class _VideoPlayerPageState extends State<VideoPlayerPage> {
  late VideoPlayerController _controller;
  late Future<void> _initializeVideoPlayerFuture;

  @override
  void initState() {
    super.initState();
    
    // 创建视频播放控制器
    _controller = VideoPlayerController.file(File(widget.videoPath));
    
    // 初始化视频播放器
    _initializeVideoPlayerFuture = _controller.initialize().then((_) {
      // 确保在初始化完成后刷新状态
      setState(() {});
      // 开始播放视频
      _controller.play();
      // 循环播放
      _controller.setLooping(true);
    });
  }

  @override
  void dispose() {
    // 确保在页面销毁时释放控制器
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar:  CustomAppBar(
        title: "发布帖子",
        onBack: () {
        Get.back();
        },
      ),
      body: FutureBuilder(
        future: _initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            // 如果视频播放器初始化完成，显示视频播放器
            return Center(
              child: AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              ),
            );
          } else {
            // 如果视频播放器还在初始化，显示加载指示器
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
  
    );
  }
}